package itd.newbee.mall.newbeemall.config.handler;

import io.swagger.v3.oas.annotations.Parameter;
import itd.newbee.mall.newbeemall.Dao.MallUserMapper;
import itd.newbee.mall.newbeemall.Dao.NewBeeMallUserTokenMapper;
import itd.newbee.mall.newbeemall.common.Constants;
import itd.newbee.mall.newbeemall.common.NewBeeMallException;
import itd.newbee.mall.newbeemall.common.ServiceResultEnum;
import itd.newbee.mall.newbeemall.config.annotation.TokenToMallUser;
import itd.newbee.mall.newbeemall.entity.MallUser;
import itd.newbee.mall.newbeemall.entity.MallUserToken;
import org.apache.el.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.rmi.server.ServerCloneException;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/3/11 17:59
 */
@Component
//快递员TokenToMallUserMethodArgumentResolver实现快递员HandlerMethodArgumentResolver

public class TokenToMallUserMethodArgumentResolver  implements HandlerMethodArgumentResolver {
    @Autowired
    private MallUserMapper mallUserMapper;
    @Autowired
    private NewBeeMallUserTokenMapper newBeeMallUserTokenMapper;
    public TokenToMallUserMethodArgumentResolver(){}
    //解析含有TokenToMallUser注解的参数
    public boolean supportsParameter(MethodParameter parameter) {
//        支持的快递类型（支持解析的带有@TokenToMallUser的参数）
        if(parameter.hasParameterAnnotation(TokenToMallUser.class)){
            return true;

        }
        return false;
    }
//    返回一个MallUser对象
    public Object resolveArgument(MethodParameter parameter,
                                  ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest,
                                  WebDataBinderFactory binderFactory) {
//得到对应的参数
            if (parameter.getParameterAnnotation(TokenToMallUser.class) instanceof TokenToMallUser){
                MallUser mallUser = null;
                String token = webRequest.getHeader("token");
                if (token != null && !"".equals(token) && token.length() == Constants.TOKEN_LENGTH) {
                    MallUserToken mallUserToken = newBeeMallUserTokenMapper.selectByToken(token);
                    if (mallUserToken != null && mallUserToken.getExpireTime().getTime()
                            <= System.currentTimeMillis()) {
//                        token过期
                       NewBeeMallException.fail(ServiceResultEnum.TOKEN_EXPIRE_ERROR.getResult());
                   }
                    mallUser = mallUserMapper.selectByPrimaryKey(mallUserToken.getUserId());
                    if(mallUser == null) {
                        // 查不到这个用户
                        NewBeeMallException.fail(ServiceResultEnum.USER_NULL_ERROR.getResult());
                    }
                    if(mallUser.getLockedFlag().intValue() == 1) {
                        // 用户锁定
                        NewBeeMallException.fail(ServiceResultEnum.LOGIN_USER_LOCKED_ERROR.getResult());
                    }
                    return mallUser;
                }else {
                    NewBeeMallException.fail(ServiceResultEnum.NOT_LOGIN_ERROR.getResult());
                }
            }
            return null;

    }

}
